package com.tuling.jvm;

/**
 * Created by chenhs on 2018/8/3.
 * <p>
 * 【创建线程导致内存溢出异常】
 *
 * VM Args：-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError -Xss2M
 */
public class JavaVMStackOOM {

    private void dontStop() {
    }

    public void stackLeakByThread() {
        while (true) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    dontStop();
                }
            });

            thread.start();
        }
    }

    public static void main(String[] args) {
        JavaVMStackOOM oom = new JavaVMStackOOM();
        oom.stackLeakByThread();

        /**
         Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
         at com.tuling.jvm.JavaVMStackOOM.stackLeakByThread(JavaVMStackOOM.java:15)
         at com.tuling.jvm.JavaVMStackOOM.main(JavaVMStackOOM.java:28)
         */
    }

}
